#######################################################
#######################################################
#######################################################
### CREATED BY JONATHAN KING AND JESSICA SCHOENHERR ###
##### REPLICATION DATA FOR "A MATTER OF OPINION?" #####
#######################################################
#######################################################
#######################################################

library(readxl)
library(tidyverse)
library(ggeffects)
library(stargazer)
library(poliscidata)
library(ggplot2)
library(psych)

# pull in the short sample data
testerData <- read_excel("Tester20210415.xlsx")

# drop the row with the questions
testerData <- testerData %>% filter(StartDate != "Start Date")

# take out the no consents
# lose 14 observations
testerData <- testerData %>% filter(Q1 == "I consent")

# drop out the people who failed the attention check
# takes us down to 104 observations
testerData <- testerData %>% filter(Q11 == "I did not read about an election scenario.")

# make a variable for the small set because the question about the opinion does not have the gender/ideology prompt
testerData$smallSample <- 1

# pull in the full sample data
biggerData <- read_excel("Full20210415.xlsx")

# drop the row with the questions
biggerData <- biggerData %>% filter(StartDate != "Start Date")

# drop the no consents
biggerData <- biggerData %>% filter(Q1 == "I consent")

# drop thep eople who failed the attention check
biggerData <- biggerData %>% filter(Q11 == "I did not read about an election scenario.")

biggerData$smallSample <- 0

# merge the two datasets together
data <- rbind(testerData, biggerData)

# convert the duration data to make it work later
data$duration <- as.numeric(data$`Duration (in seconds)`)

# drop the original variable
data <- data %>% select(-`Duration (in seconds)`)

##########################
### LEGITIMACY BATTERY ###
##########################

# from Gibson, Caldeira, and Spence 2003
# specific support: 
#	job in government (general approval)
#	too liberal/conservative (general satisfaction with policy)
#	plus questions about policies, but we're not using those
# confidence
#	How much confidence do you have in the U.S. Supreme Court?
# 		this is from the GSS
# six questions for overall "loyalty" or legitimacy


# Q2
# How well do you think the U.S. Supreme Court does its main job in government? Would you say it does a great job, a pretty good job, not a very good job, or a poor job?

data$specificQ1DoesMainJob <- ifelse(data$Q2 == "Poor job", 1, 
	ifelse(data$Q2 == "Not a very good job", 2, 
	ifelse(data$Q2 == "Pretty good job", 3, 
	ifelse(data$Q2 == "Great job", 4, NA))))

# Q3
# In general, would you say that the Supreme Court is too liberal, or too conservative, or about just right in its decisions?

data$specificQ2TooLiberal <- ifelse(data$Q3 == "Much too liberal", 1, 
	ifelse(data$Q3 == "Too liberal", 2, 
	ifelse(data$Q3 == "Just right", 3, 
	ifelse(data$Q3 == "Too conservative", 4, 
	ifelse(data$Q3 == "Much too conservative", 5, NA)))))

# Q4
# How much confidence do you have in the U.S. Supreme Court?

data$confidence <- ifelse(data$Q4 == "Hardly any confidence", 1, 
	ifelse(data$Q4 == "Only some confidence", 2, 
	ifelse(data$Q4 == "A great deal of confidence", 3, NA)))

# Q5
# If the U.S. Supreme Court started making a lot of decisions that most people disagree with, it might be better to do away with the Supreme Court altogether.
	# disagree is the supportive answer, so code backward

data$legitQ1DoAway <- ifelse(data$Q5 == "Disagree strongly", 5, 
	ifelse(data$Q5 == "Disagree somewhat", 4, 
	ifelse(data$Q5 == "Neither agree nor disagree", 3, 
	ifelse(data$Q5 == "Agree somewhat", 2, 
	ifelse(data$Q5 == "Agree strongly", 1, NA)))))

# Q6
# The right of the Supreme Court to decide certain types of controversial issues should be reduced.
	# disagree is the supportive answer, so code backward

data$legitQ2Controversy <- ifelse(data$Q6 == "Disagree strongly", 5, 
	ifelse(data$Q6 == "Disagree somewhat", 4, 
	ifelse(data$Q6 == "Neither agree nor disagree", 3, 
	ifelse(data$Q6 == "Agree somewhat", 2, 
	ifelse(data$Q6 == "Agree strongly", 1, NA)))))

# Q7
# The Supreme Court can usually be trusted to make decisions that are right for the country as a whole.
	# agree is the supportive answer

data$legitQ3Trust <- ifelse(data$Q7 == "Disagree strongly", 1, 
	ifelse(data$Q7 == "Disagree somewhat", 2, 
	ifelse(data$Q7 == "Neither agree nor disagree", 3, 
	ifelse(data$Q7 == "Agree somewhat", 4, 
	ifelse(data$Q7 == "Agree strongly", 5, NA)))))

# Q8
# The decisions of the U.S. Supreme Court favor some groups more than others.
	# supportive answer is disagree, so code backward

data$legitQ4FavorGroups <- ifelse(data$Q8 == "Disagree strongly", 5, 
	ifelse(data$Q8 == "Disagree somewhat", 4, 
	ifelse(data$Q8 == "Neither agree nor disagree", 3, 
	ifelse(data$Q8 == "Agree somewhat", 2, 
	ifelse(data$Q8 == "Agree strongly", 1, NA)))))

# Q9
# The U.S. Supreme Court gets too mixed up in politics.
	# supportive answer is disagree, so code backward

data$legitQ5Politics <- ifelse(data$Q9 == "Disagree strongly", 5, 
	ifelse(data$Q9 == "Disagree somewhat", 4, 
	ifelse(data$Q9 == "Neither agree nor disagree", 3, 
	ifelse(data$Q9 == "Agree somewhat", 2, 
	ifelse(data$Q9 == "Agree strongly", 1, NA)))))

# Q10
# The U.S. Supreme Court should have the right to say what the Constitution means, even when the majority of the people disagree with the Court's decision.
	# supprotive answer is agree

data$legitQ6Constitution <- ifelse(data$Q10 == "Disagree strongly", 1, 
	ifelse(data$Q10 == "Disagree somewhat", 2, 
	ifelse(data$Q10 == "Neither agree nor disagree", 3, 
	ifelse(data$Q10 == "Agree somewhat", 4, 
	ifelse(data$Q10 == "Agree strongly", 5, NA)))))

# drop out all the columns we don't need anymore
data <- data %>% select(-StartDate, -EndDate, -Status, -IPAddress, -Progress, -Finished, -RecordedDate, -RecipientLastName, -RecipientFirstName, -RecipientEmail, -ExternalReference, -LocationLatitude, -LocationLongitude, -DistributionChannel, -UserLanguage, -Q1, -Q2, -Q3, -Q4, -Q5, -Q6, -Q7, -Q8, -Q9, -Q10, -Q11)

# legitimacy scale is summative
# increased score = higher support for the Court
# six questions = high of 30, low of 5
data$legitScore <- data$legitQ1DoAway + data$legitQ2Controversy + data$legitQ3Trust + data$legitQ4FavorGroups + data$legitQ5Politics + data$legitQ6Constitution

data$legitAverage <- data$legitScore/6

# look at it using the psych package
# average legit score
legit <- data[c("legitQ1DoAway", "legitQ2Controversy", "legitQ3Trust", "legitQ4FavorGroups", "legitQ5Politics", "legitQ6Constitution")]

psych::alpha(legit)

###########################
### KNOWLEDGE QUESTIONS ###
###########################

# Who is the current Chief Justice of the United States?
data$knowsCJ <- ifelse(is.na(data$Q12), NA, 
	ifelse(data$Q12 == "John Roberts", 1, 0))

# keep the data
table(data$Q12)

# Some judges in the U.S. are elected; others are appointed to the bench. Do you happen to know if the justices of the U.S. Supreme Court are elected or appointed to the bench?

data$appointed <- ifelse(is.na(data$Q13), NA, ifelse(data$Q13 == "Appointed", 1, 0))

# Some judges in the U.S. serve for a set number of years; others serve a life term. Do you happen to know whether the justices of the U.S. Supreme Court serve for a set number of years or whether they serve a life term?

data$lifeTerm <- ifelse(is.na(data$Q14), NA, ifelse(data$Q14 == "Life term", 1, 0))

# Do you happen to know who has the last say when there is a conflict over the meaning of the Constitution -- the U.S. Supreme Court, the U.S. Congress, or the President?

data$lastSay <- ifelse(is.na(data$Q15), NA, ifelse(data$Q15 == "U.S. Supreme Court", 1, 0))

# Do you happen to know if the Supreme Court has made decisions on gay marriage?

data$gayMarriage <- ifelse(is.na(data$Q16), NA, ifelse(data$Q16 == "Yes, it has", 1,
 	ifelse(data$Q16 == "No, it has not", -1, 0)))

# Do you happen to know if the Supreme Court has made decisions on the rights of Black Americans?
data$blackRights <- ifelse(is.na(data$Q17), NA, ifelse(data$Q17 == "Yes, it has", 1,
 	ifelse(data$Q17 == "No, it has not", -1, 0)))

# Do you happen to know if the Supreme Court has made decisions on the maximum income tax rate?

data$taxation <- ifelse(is.na(data$Q18), NA, ifelse(data$Q18 == "Yes, it has", -1,
 	ifelse(data$Q18 == "No, it has not", 1, 0)))

# drop out the variables we don't need anymore
data <- data %>% select(-Q13, -Q14, -Q15, -Q16, -Q17, -Q18)

##################
### EXPERIMENT ###
##################

# put everybody in their treatment group

# 1 - Abortion, liberal, male
# 2 - Abortion, conservative, male
# 3 - Abortion, liberal, female
# 4 - Abortion, conservative, female
# 5 - Abortion, control
# 6 - Death penalty, liberal, male
# 7 - Death penalty, conservative, male
# 8 - Death penalty, liberal, female
# 9 - Death penalty, conservative, female
# 10 - Death penalty, control

data$treatmentGroup <- ifelse(!is.na(data$`Q20_First Click`), 1, 
	ifelse(!is.na(data$`Q30_First Click`), 2, 
	ifelse(!is.na(data$`Q40_First Click`), 3, 
	ifelse(!is.na(data$`Q50_First Click`), 4, 
	ifelse(!is.na(data$`Q60_First Click`), 5, 
	ifelse(!is.na(data$`Q69_First Click`), 6, 
	ifelse(!is.na(data$`Q79_First Click`), 7, 
	ifelse(!is.na(data$`Q89_First Click`), 8, 
	ifelse(!is.na(data$`Q99_First Click`), 9, 
	ifelse(!is.na(data$`Q109_First Click`), 10, 0))))))))))

data$writerProfile <- ifelse(data$smallSample == 1, 0,
	ifelse(!is.na(data$Q21), data$Q21, 
	ifelse(!is.na(data$Q31), data$Q31, 
	ifelse(!is.na(data$Q41), data$Q41, 
	ifelse(!is.na(data$Q51), data$Q51, 
	ifelse(!is.na(data$Q70), data$Q70, 
	ifelse(!is.na(data$Q80), data$Q80, 
	ifelse(!is.na(data$Q90), data$Q90, 
	ifelse(!is.na(data$Q100), data$Q100, NA)))))))))
	
data$writerProfileCorrect <- ifelse(data$treatmentGroup == 5, NA,
	ifelse(data$treatmentGroup == 10, NA,
	ifelse(data$writerProfile == 0, NA, 
	ifelse(data$treatmentGroup == 1 & data$writerProfile == "Liberal male", 1,
	ifelse(data$treatmentGroup == 2 & data$writerProfile == "Conservative male", 1,
	ifelse(data$treatmentGroup == 3 & data$writerProfile == "Liberal female", 1, 
	ifelse(data$treatmentGroup == 4 & data$writerProfile == "Conservative female", 1,
	ifelse(data$treatmentGroup == 6 & data$writerProfile == "Liberal male", 1, 
	ifelse(data$treatmentGroup == 7 & data$writerProfile == "Conservative male", 1,
	ifelse(data$treatmentGroup == 8 & data$writerProfile == "Liberal female", 1,
	ifelse(data$treatmentGroup == 9 & data$writerProfile == "Conservative female", 1, 
	ifelse(data$writerProfile == "Don't remember", 2, 0))))))))))))

data$abortionLegal <- 
	ifelse(!is.na(data$Q22), data$Q22, 
	ifelse(!is.na(data$Q32), data$Q32, 
	ifelse(!is.na(data$Q42), data$Q42, 
	ifelse(!is.na(data$Q52), data$Q52, 
	ifelse(!is.na(data$Q61), data$Q61, 0)))))
	
data$abortionLegalRecode <- ifelse(data$abortionLegal == "Illegal in all cases", 1,
	ifelse(data$abortionLegal == "Illegal in most cases", 2,
	ifelse(data$abortionLegal == "Legal in most cases", 3, 
	ifelse(data$abortionLegal == "Legal in all cases", 4,
	ifelse(data$abortionLegal == 0 & data$treatmentGroup < 6, NA, NA)))))
	
data$abortionSupport <- ifelse(data$abortionLegalRecode >=3, 1, 0)

data$deathPenaltyFavor <- ifelse(!is.na(data$Q71), data$Q71, 
	ifelse(!is.na(data$Q81), data$Q81, 
	ifelse(!is.na(data$Q91), data$Q91, 
	ifelse(!is.na(data$Q101), data$Q101, 
	ifelse(!is.na(data$Q110), data$Q110, 0)))))
	
data$deathPenaltyFavorRecode <- ifelse(data$deathPenaltyFavor == "Strongly oppose", 1,
	ifelse(data$deathPenaltyFavor == "Oppose", 2, 
	ifelse(data$deathPenaltyFavor == "Favor", 3,
	ifelse(data$deathPenaltyFavor == "Strongly favor", 4,
	ifelse(data$deathPenaltyFavor == 0 & data$treatmentGroup > 5, NA, NA)))))
	
data$deathPenaltySupport <- ifelse(data$deathPenaltyFavorRecode >= 3, 1, 0)

data$scTherm <- ifelse(!is.na(data$Q23_1), data$Q23_1, 
	ifelse(!is.na(data$Q33_1), data$Q33_1, 
	ifelse(!is.na(data$Q43_1), data$Q43_1, 
	ifelse(!is.na(data$Q53_1), data$Q53_1, 
	ifelse(!is.na(data$Q62_1), data$Q62_1, 
	ifelse(!is.na(data$Q72_1), data$Q72_1, 
	ifelse(!is.na(data$Q82_1), data$Q82_1, 
	ifelse(!is.na(data$Q92_1), data$Q92_1, 
	ifelse(!is.na(data$Q102_1), data$Q102_1, 
	ifelse(!is.na(data$Q111_1), data$Q111_1, NA))))))))))
	
# drop out any NAs with the therm
data <- data %>% filter(!is.na(data$scTherm))

data$decisionTherm <- ifelse(!is.na(data$Q25_1), data$Q25_1, 
	ifelse(!is.na(data$Q35_1), data$Q35_1, 
	ifelse(!is.na(data$Q45_1), data$Q45_1, 
	ifelse(!is.na(data$Q55_1), data$Q55_1, 
	ifelse(!is.na(data$Q64_1), data$Q64_1, 
	ifelse(!is.na(data$Q74_1), data$Q74_1, 
	ifelse(!is.na(data$Q84_1), data$Q84_1, 
	ifelse(!is.na(data$Q94_1), data$Q94_1, 
	ifelse(!is.na(data$Q104_1), data$Q104_1, 
	ifelse(!is.na(data$Q113_1), data$Q113_1, NA))))))))))
	
# drop out the NAs
data <- data %>% filter(!is.na(data$decisionTherm))

data$agreeOrDisagreeDecision <- ifelse(!is.na(data$Q27), data$Q27, 
	ifelse(!is.na(data$Q37), data$Q37, 
	ifelse(!is.na(data$Q47), data$Q47, 
	ifelse(!is.na(data$Q57), data$Q57, 
	ifelse(!is.na(data$Q66), data$Q66, 
	ifelse(!is.na(data$Q76), data$Q76, 
	ifelse(!is.na(data$Q86), data$Q86, 
	ifelse(!is.na(data$Q96), data$Q96, 
	ifelse(!is.na(data$Q106), data$Q106, 
	ifelse(!is.na(data$Q115), data$Q115, NA))))))))))
	
data$agreeWithDecisionRecode <- ifelse(data$agreeOrDisagreeDecision == "Agree", 1,
	ifelse(data$agreeOrDisagreeDecision == "Disagree", -1, 0))

data$shouldBeDeciding <- ifelse(!is.na(data$Q28), data$Q28, 
	ifelse(!is.na(data$Q38), data$Q38, 
	ifelse(!is.na(data$Q48), data$Q48, 
	ifelse(!is.na(data$Q58), data$Q58, 
	ifelse(!is.na(data$Q77), data$Q67, 
	ifelse(!is.na(data$Q87), data$Q87, 
	ifelse(!is.na(data$Q97), data$Q97, 
	ifelse(!is.na(data$Q107), data$Q107, 
	ifelse(!is.na(data$Q116), data$Q116, NA)))))))))

data$shouldBeDecidingRecode <- ifelse(data$shouldBeDeciding == "Yes, should be", 1, 
	ifelse(data$shouldBeDeciding == "No, should not be", -1,
	ifelse(data$shouldBeDeciding == "No opinion", 0, NA)))

data$abortion <- ifelse(data$treatmentGroup < 6, 1, 0)
data$deathPenalty <- ifelse(data$treatmentGroup > 5, 1, 0)

# drop out all the raw data I just got rid of
data <- data %>% select(-`Q20_First Click`, -`Q20_Last Click`, -`Q20_Page Submit`, -`Q20_Click Count`, -Q21, -Q22, -Q23_1, -`Q24_First Click`, -`Q24_Last Click`, -`Q24_Page Submit`, -`Q24_Click Count`, -Q25_1, -`Q26_First Click`, -`Q26_Last Click`, -`Q26_Page Submit`, -`Q26_Click Count`, -Q27, -Q28, -`Q30_First Click`, -`Q30_Last Click`, -`Q30_Page Submit`, -`Q30_Click Count`, -Q31, -Q32, -Q33_1, -`Q34_First Click`, -`Q34_Last Click`, -`Q34_Page Submit`, -`Q34_Click Count`, -Q35_1, -`Q36_First Click`, -`Q36_Last Click`, -`Q36_Page Submit`, -`Q36_Click Count`, -Q37, -Q38, -`Q40_First Click`, -`Q40_Last Click`, -`Q40_Page Submit`, -`Q40_Click Count`, -Q41, -Q42, -Q43_1, -`Q44_First Click`, -`Q44_Last Click`, -`Q44_Page Submit`, -`Q44_Click Count`, -Q45_1, -`Q46_First Click`, -`Q46_Last Click`, -`Q46_Page Submit`, -`Q46_Click Count`, -Q47, -Q48, -`Q50_First Click`, -`Q50_Last Click`, -`Q50_Page Submit`, -`Q50_Click Count`, -Q51, -Q52, -Q53_1, -`Q54_First Click`, -`Q54_Last Click`, -`Q54_Page Submit`, -`Q54_Click Count`, -Q55_1, -`Q56_First Click`, -`Q56_Last Click`, -`Q56_Page Submit`, -`Q56_Click Count`, -Q57, -Q58, -`Q60_First Click`, -`Q60_Last Click`, -`Q60_Page Submit`, -`Q60_Click Count`, -Q61, -Q62_1, -`Q63_First Click`, -`Q63_Last Click`, -`Q63_Page Submit`, -`Q63_Click Count`, -Q64_1, -`Q65_First Click`, -`Q65_Last Click`, -`Q65_Page Submit`, -`Q65_Click Count`, -Q66, -Q67, -`Q69_First Click`, -`Q69_Last Click`, -`Q69_Page Submit`, -`Q69_Click Count`, -Q70, -Q71, -Q72_1, -`Q73_First Click`, -`Q73_Last Click`, -`Q73_Page Submit`, -`Q73_Click Count`, -Q74_1, -`Q75_First Click`, -`Q75_Last Click`, -`Q75_Page Submit`, -`Q75_Click Count`, -Q76, -Q77, -`Q79_First Click`, -`Q79_Last Click`, -`Q79_Page Submit`, -`Q79_Click Count`, -Q80, -Q81, -Q82_1, -`Q83_First Click`, -`Q83_Last Click`, -`Q83_Page Submit`, -`Q83_Click Count`, -Q84_1, -`Q85_First Click`, -`Q85_Last Click`, -`Q85_Page Submit`, -`Q85_Click Count`, -Q86, -Q87, -`Q89_First Click`, -`Q89_Last Click`, -`Q89_Page Submit`, -`Q89_Click Count`, -Q90, -Q91, -Q92_1, -`Q93_First Click`, -`Q93_Last Click`, -`Q93_Page Submit`, -`Q93_Click Count`, -Q94_1, -`Q95_First Click`, -`Q95_Last Click`, -`Q95_Page Submit`, -`Q95_Click Count`, -Q96, -Q97, -`Q99_First Click`, -`Q99_Last Click`, -`Q99_Page Submit`, -`Q99_Click Count`, -Q100, -Q101, -Q102_1, -`Q103_First Click`, -`Q103_Last Click`, -`Q103_Page Submit`, -`Q103_Click Count`, -Q104_1, -`Q105_First Click`, -`Q105_Last Click`, -`Q105_Page Submit`, -`Q105_Click Count`, -Q106, -Q107, -`Q109_First Click`, -`Q109_Last Click`, -`Q109_Page Submit`, -`Q109_Click Count`, -Q110, -Q111_1, -`Q112_First Click`, -`Q112_Last Click`, -`Q112_Page Submit`, -`Q112_Click Count`, -Q113_1, -`Q114_First Click`, -`Q114_Last Click`, -`Q114_Page Submit`, -`Q114_Click Count`, -Q115, -Q116)

########################
### DEMOGRAPHIC DATA ###
########################

# gender: 1 = Male, 2 = Female
data$female <- ifelse(data$gender == 1, 0,
	ifelse(data$gender == 2, 1, NA))
	
# political_party
data$democrat <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 1 | data$political_party == 2 | data$political_party == 3 | data$political_party == 6, 1, 0))
	
data$republican <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 10 | data$political_party == 9 | data$political_party == 8 | data$political_party == 5, 1, 0))
	
data$independent <- ifelse(is.na(data$political_party), NA,
	ifelse(data$political_party == 4 | data$political_party == 7, 1, 0))

# ethnicity
data$white <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 1, 1, 0))

data$black <- ifelse(is.na(data$ethnicity), NA, 
	ifelse(data$ethnicity == 2, 1, 0))

data$notWhite <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity != 1, 1, 0))
	
data$asianPI <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 4, 1, 
	ifelse(data$ethnicity == 5, 1, 
	ifelse(data$ethnicity == 6, 1,
	ifelse(data$ethnicity == 7, 1,
	ifelse(data$ethnicity == 8, 1,
	ifelse(data$ethnicity == 9, 1,
	ifelse(data$ethnicity == 10, 1,
	ifelse(data$ethnicity == 11, 1,
	ifelse(data$ethnicity == 12, 1,
	ifelse(data$ethnicity == 13, 1,
	ifelse(data$ethnicity == 14, 1, 0))))))))))))
	
data$nativeAmerican <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 3, 1, 0))
	
data$otherRace <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 15, 1, 0))

data$noRaceAnswer <- ifelse(is.na(data$ethnicity), NA,
	ifelse(data$ethnicity == 16, 1, 0))
	
# hhi
data$income <- ifelse(data$hhi == -3105, NA, data$hhi)
data$income <- as.numeric(data$income)

# education
data$education <- ifelse(data$education == -3105 | data$education == 10 | data$education == 11 | data$education == 12, NA, data$education)
data$education <- as.numeric(data$education)

# age
data$age <- as.numeric(data$age)

# groups
data$demoGroup <- ifelse(data$female == 0 & data$democrat == 1, 1,
	ifelse(data$female == 0 & data$republican == 1, 2,
	ifelse(data$female == 0 & data$independent == 1, 3,
	ifelse(data$female == 1 & data$democrat == 1, 4,
	ifelse(data$female == 1 & data$republican == 1, 5,
	ifelse(data$female == 1 & data$independent == 1, 6, 0))))))
data$demoGroup <- as.factor(data$demoGroup)

data$partisanship <- ifelse(data$democrat == 1, 1,
	ifelse(data$independent == 1, 2,
	ifelse(data$republican == 1, 3, 0)))
	
data <- data %>% filter(!is.na(data$partisanship))

################
### DURATION ###
################

# duration drop
# drop out anyone who took the survey faster than 1/3 of the median time
describe(data$duration)

# median is 537.0
537/3
# 179

# drop anyone who finished the survey in less than 179 seconds
data <- data %>% filter(duration >= 179)

# lost 15 observations

##############
### SPLITS ###
##############

# split the data, one set for abortion, one for death penalty

dataAbortion <- data %>% filter(treatmentGroup < 6)
dataDeathPenalty <- data %>% filter(treatmentGroup > 5)

####################
### DEMOGRAPHICS ###
####################

describe(data$age)
wtd.sd(data$age)

describe(data$female)

describe(data$income)
wtd.sd(data$income)

table(data$white)
table(data$black)
table(data$asianPI)
table(data$nativeAmerican)
table(data$otherRace)
table(data$noRaceAnswer)

table(data$democrat)
table(data$republican)
table(data$independent)

describe(data$education)
wtd.sd(data$education)

##########################
### WRITE OUT THE DATA ###
##########################

write.csv(dataAbortion, "AbortionData20210806.csv")
write.csv(dataDeathPenalty, "DeathPenaltyData20210806.csv")
